/***************************************************************************
 *   Copyright (C) 2007 by Warren Kenny   *
 *   warren.kenny@gmail.com   *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/
#include <otaClientCredentials.h>
#include <otaMySQLCommonHeader.h>

otaClientCredentials::otaClientCredentials()
{
}

bool otaClientCredentials::deserialize( const char *data, unsigned int dataSize, const unsigned int offset, unsigned int &newOffset )
{
	otaMySQLCommonHeader header;
	if(header.deserialize( data, dataSize, offset, newOffset ))
	{
		if( (dataSize - offset >= OTA_CLIENT_CREDENTIALS_MINIMUM_LENGTH) && ( header.getSequenceNumber() == 1 ) )
		{
			if(verifyCredentials( data, dataSize, offset ) )
			{
				std::cout << "received client credentials" << std::endl;
				char filler;
				bool ok = (	otaBytePacker::removeBasicType( data, dataSize, offset, newOffset, m_clientFlags ) &&
						otaBytePacker::removeBasicType( data, dataSize, newOffset, newOffset, m_maxPacketSize ) &&
						otaBytePacker::removeBasicType( data, dataSize, newOffset, newOffset, m_characterSetNumber ) &&
						otaBytePacker::removeNTString( data, dataSize, newOffset, newOffset, m_userName ) &&
						otaBytePacker::removeNTString( data, dataSize, newOffset, newOffset, m_scrambleBuffer ) &&
						otaBytePacker::removeBasicType( data, dataSize, newOffset, newOffset, filler ));
				
				ok = ok && (filler == 0x00);
				
				if( ok )
				{
					if( dataSize - newOffset > 0 )
					{
						ok = ok && otaBytePacker::removeNTString( data, dataSize, newOffset, newOffset, m_database );
					}
					else
					{
						m_database = "No Database Selected";
					}
				}
				return ok;		
			}
		}
	}
	return false;
}

bool otaClientCredentials::verifyCredentials( const char *data, unsigned int dataSize, const unsigned int offset )
{
	const char fillerPattern = 0x00;
	char filler;
	bool ok = true;
	unsigned int newOffset = offset;
	ok = ok && otaBytePacker::removeBasicType( data, dataSize, 9, newOffset, filler );
	if( (filler == fillerPattern) && ok )
	{
		return true;
	}
	return false;
}

std::string otaClientCredentials::getUsername()
{
	return m_userName;
}
	
std::string otaClientCredentials::getDatabase()
{
	return m_database;
}
	
unsigned int otaClientCredentials::getClientFlags()
{
	return m_clientFlags;
}
	
otaClientCredentials::~otaClientCredentials()
{
}


